<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>企业职员管理系统 - 登录</title>
    <link rel="icon" href="favicon.ico" type="image/x-icon"/>
    <!-- Font Awesome 图标 -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        :root{
            --primary: #3498db;
            --secondary: #2ecc71;
        }

        body {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            /* 设置背景图片 */
            background: url('loginBackground.jpg');/*no-repeat center center fixed;*/
            background-size: cover;
            /* 添加背景图片的备用颜色 */
            background-color: #f5f7fa;
        }

        .login-container {
            background: #fff;
            padding: 40px;
            border-radius: 20px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
            width: 100%;
            max-width: 400px;
            transition: transform 0.3s;
        }

        .login-container:hover {
            transform: translateY(-5px);
        }

        /*.login-container::before{*/
        /*    content: '';                         !* 伪元素必需属性 *!*/
        /*    position: absolute;                  !* 绝对定位 *!*/
        /*    top: 0;                              !* 定位到容器顶部 *!*/
        /*    left: 0;*/
        /*    width: 100%;                         !* 与容器同宽 *!*/
        /*    height: 5px;                         !* 5像素高的装饰条 *!*/
        /*    background: linear-gradient(90deg, var(--primary), var(--secondary), var(--primary));*/
        /*    background-size: 200% 200%;          !* 扩大背景尺寸用于动画 *!*/
        /*    animation: gradientBar 3s ease infinite; !* 应用无限循环动画 *!}*/

        /*@keyframes gradientBar {*/
        /*    0% { background-position: 0% 50%; }*/
        /*    50% { background-position: 100% 50%; }*/
        /*    100% { background-position: 0% 50%; }*/
        /*}*/

        .logo {
    text-align: center;
    margin-bottom: 30px;
    gap: 12px;

    background: linear-gradient(90deg, #3498db, #2980b9);
    -webkit-background-clip: text;
    background-clip: text;
}


.logo i {
    font-size: 2.5rem;
    color: var(--dark);
            margin-bottom: 15px;
            background: linear-gradient(135deg, var(--primary), var(--secondary));
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
}

.logo h2 {
    font-size: 1.8rem;
    font-weight: 700;
    letter-spacing: 0.5px;
}
.submit-btn {
            width: 100%;
            padding: 16px;
            background: linear-gradient(135deg, var(--primary), var(--secondary));
            color: white;
            border: none;
            border-radius: 10px;
            font-size: 17px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s;
            margin-top: 15px;
            position: relative;
            overflow: hidden;
            box-shadow: 0 5px 15px rgba(52, 152, 219, 0.3);
        }

        .submit-btn:hover {
            transform: translateY(-3px);
            box-shadow: 0 8px 20px rgba(52, 152, 219, 0.4);
        }

        .submit-btn:active {
            transform: translateY(0);
        }
        .form-group {
            margin-bottom: 20px;
            position: relative;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #2c3e50;
            font-weight: 500;
        }

        .input-icon {
            position: relative;
        }

        .input-icon i {
            position: absolute;
            left: 15px;
            top: 50%;
            transform: translateY(-50%);
            color: #7f8c8d;
        }

        input {
            width: 100%;
            padding: 12px 15px 12px 40px;
            border: 2px solid #e0e0e0;
            border-radius: 8px;
            font-size: 16px;
            transition: border-color 0.3s;
        }

        input:focus {
            outline: none;
            border-color: #3498db;
        }

        button {
            width: 100%;
            padding: 12px;
            background: #3498db;
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: 500;
            cursor: pointer;
            transition: background 0.3s;
        }

        button:hover {
            background: #2980b9;
        }

        .captcha-container {
            display: flex;
            align-items: center;
            margin-bottom: 20px;
        }

        .captcha-input {
            flex: 1;
            margin-right: 10px;
        }

        .captcha-display {
            font-size: 24px;
            font-weight: bold;
            letter-spacing: 5px;
            padding: 10px 15px;
            background: #f8f9fa;
            border-radius: 8px;
            border: 2px solid #e0e0e0;
            cursor: pointer;
            user-select: none;
        }

        .captcha-display:hover {
            border-color: #3498db;
        }

        .links {
            margin-top: 20px;
            text-align: center;
        }

        .links a {
            color: #7f8c8d;
            text-decoration: none;
            font-size: 14px;
            transition: color 0.3s;
        }

        .links a:hover {
            color: #3498db;
        }

        @media (max-width: 480px) {
            .login-container {
                margin: 20px;
                padding: 25px;
            }
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="logo">
            <i class="fas fa-users-cog"></i>
            <h2>公司职员管理系统</h2>
        </div>

        <form id="loginForm" onsubmit="return validateForm()">
            <div class="form-group">
                <label for="username">用户名</label>
                <div class="input-icon">
                    <i class="fas fa-user"></i>
                    <input type="text"
                           id="username"
                           name="username"
                           placeholder="请输入用户名"
                           required>
                </div>
            </div>

            <div class="form-group">
                <label for="password">密码</label>
                <div class="input-icon">
                    <i class="fas fa-lock"></i>
                    <input type="password"
                           id="password"
                           name="password"
                           placeholder="请输入密码"
                           required
                           minlength="6">
                </div>
            </div>

            <div class="form-group">
                <label>验证码</label>
                <div class="captcha-container">
                    <div class="captcha-input">
                        <input type="text"
                               id="captchaInput"
                               name="captcha"
                               placeholder="请输入验证码"
                               required
                               maxlength="4">
                    </div>
                    <div class="captcha-display" id="captchaDisplay" onclick="generateCaptcha()">
                        <!-- 验证码将在这里显示 -->
                    </div>
                </div>
            </div>

            <button type="submit" class="submit-btn">登 录</button>

            <div class="links">
                <a href="#forgot-password">忘记密码？</a>
                <span style="margin: 0 10px">|</span>
                <a href="/company/register">注册账号</a>
            </div>
        </form>
    </div>

    <script>
        // 生成随机验证码
        function generateCaptcha() {
            const captchaDisplay = document.getElementById('captchaDisplay');
            const captchaInput = document.getElementById('captchaInput');

            // 生成4位随机数字验证码
            const captcha = Math.floor(1000 + Math.random() * 9000);
            captchaDisplay.textContent = captcha;
            captchaInput.value = '';
            window.currentCaptcha = captcha;
        }

        // 验证表单
        function validateForm() {
            const username = document.getElementById('username').value.trim();
            const password = document.getElementById('password').value;
            const captchaInput = document.getElementById('captchaInput').value;
            const submitBtn = document.querySelector('button[type="submit"]');

            // 前端验证
            if (!username) {
                alert('用户名不能为空');
                return false;
            }
            if (password.length < 6) {
                alert('密码长度不能小于6位');
                return false;
            }
            if (captchaInput !== window.currentCaptcha?.toString()) {
                alert('验证码错误，请重新输入');
                generateCaptcha();
                return false;
            }

            // 禁用按钮防止重复提交
            submitBtn.disabled = true;
            submitBtn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> 登录中...';

            // 发送AJAX请求
            fetch('login1', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ username, password })
            })
                .then(response => {
                    if (!response.ok) throw new Error('HTTP 错误');
                    return response.text(); // 先尝试以 text() 获取响应
                })
                .then(text => {
                    try {
                        const data = JSON.parse(text); // 再手动解析为 JSON
                        if (data.success) {
                            window.location.href = data.redirectUrl || '/index';
                        } else {
                            alert(data.message || '登录失败');
                            generateCaptcha();
                        }
                    } catch (e) {
                        console.error('JSON 解析失败:', e);
                        console.log('原始响应:', text);
                        alert('服务器返回非 JSON 数据，请检查接口实现');
                    }
                })
                .catch(error => {
                    console.error('请求失败:', error);
                    alert('网络请求失败，请检查网络连接');
                });

            return false; // 阻止表单默认提交
        }

        // 页面加载时生成初始验证码
        window.onload = generateCaptcha;
    </script>
</body>
</html>